import React, { useEffect, useRef } from 'react'

function useLoash(call, delay) {
    const timer = useRef(null)
    useEffect(() => {
        return () => {
            if (timer.current) {
                clearTimeout(timer.current)
            }
        }
    }, [])
    return (...args) => {
        if (timer.current) {
            clearTimeout(timer.current)
        }
        timer.current = setTimeout(() => {
            call(...args)
        }, delay)
    }
}

export default useLoash
